import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta:{
      TabbarShow:true
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta:{
      TabbarShow:false
    },
    beforeEnter(to, from, next) {
      const { isLogin } = localStorage;
      isLogin ? next({ name: "Home" }) : next();
      // next();继续展示当前页面
    },
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta:{
      TabbarShow:false
    },
    beforeEnter(to, from, next) {
      const { isLogin } = localStorage;
      isLogin ? next({ name: "Home" }) : next();
    },
  },
  {
    path: '/category',
    name: 'category',
    component: () => import('../views/Category.vue'),
    meta:{
      TabbarShow:true
    }
  },
  {
    path: '/shopping',
    name: 'shopping',
    component: () => import('../views/ShoppingView.vue'),
    meta:{
      TabbarShow:true
    }
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import('../views/MineView.vue'),
    meta:{
      TabbarShow:true
    }
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/SearchView.vue'),
  },
  {
    path: '/mine/addressList',
    name: 'addressList',
    component: () => import('../views/AddressList.vue')
  },
  {
    path: '/mine/addressEdit',
    name: 'addressEdit',
    component: () => import('../views/AddressEdit.vue')
  }
]

 const router = new VueRouter({
   mode: 'hash',
   base: process.env.BASE_URL,
   routes
 })


// 创建路由实例
// const router = createRouter({
//   //创建hash历史记录
//   history: createWebHashHistory(),
//   routes,
// });
// // 何时执行：页面跳转之前（路由切换之前）
// router.beforeEach((to, from, next) => {
//   // 解构赋值
//   // 相当于const isLogin = localStorage.isLogin
//   const { isLogin } = localStorage;
//   const { name } = to;
//   // next();继续展示当前页面
//   const isLoginOrRegister = name === "Login" || name === "Register";
//   isLogin || isLoginOrRegister ? next() : next({ name: "Login" });
// });
export default router
